package com.aidong.login.biz.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aidong.login.biz.MemberIntegralBiz;
import com.aidong.login.dao.MemberIntegralDao;
import com.aidong.login.dao.MemberIntegralLogDao;
import com.aidong.login.entity.MemberIntegral;
import com.aidong.login.entity.MemberIntegralLog;
import com.aidong.login.entity.MemberWithdraw;
import com.aidong.util.IntegralAction;

@Service("memberIntegralBiz")
public class MemberIntegralBizImpl implements MemberIntegralBiz {
	@Autowired
	private MemberIntegralDao integralDao;
	@Autowired
	private MemberIntegralLogDao logDao;

	@Override
	public MemberIntegral getMemberIntegral(int idongId) {
		return integralDao.getIntegral(idongId);
	}

	@Transactional
	@Override
	public void increase(IntegralAction action, int idongId, int amount, String remark) {
		integralDao.increase(idongId, amount);
		logDao.addIntegralLog(new MemberIntegralLog(idongId, action.getActionValue(), amount, remark));

	}

	@Transactional
	@Override
	public void reduce(IntegralAction action, int idongId, int amount, String remark) {
		integralDao.increase(idongId, -amount);
		logDao.addIntegralLog(new MemberIntegralLog(idongId, action.getActionValue(), -amount, remark));
	}

	@Transactional
	@Override
	public void withdraw(MemberWithdraw withdraw) {
		integralDao.increase(withdraw.getIdongId(), -(withdraw.getAmount() * 10));
		logDao.addIntegralLog(new MemberIntegralLog(withdraw.getIdongId(), IntegralAction.withdraw.getActionValue(),
				-(withdraw.getAmount() * 10), ""));
	}

	@Override
	public void increase(int idongId, int amount) {
		integralDao.increase(idongId, amount);
	}

}
